<template>
    <div>
        <div class="wrapper">
            <div
                style="margin: 200px auto; background-color: #ede6e6;opacity: 0.8; width: 350px; height: 440px; padding: 20px; border-radius: 10px">
                <div style="margin-top: 0px; text-align: center; font-size:     ;padding: 0;"><b>欢迎来到宠物医院管理系统</b></div>
                <div style="margin-top: 10px; text-align: center; font-size: 24px"><b>注册</b></div>
                <el-form :model="user" :rules="rules" ref="userForm">
                    <el-form-item prop="username" label="用户名" label-width="70px">
                        <el-input size="medium" prefix-icon="el-icon-user" v-model="user.username"></el-input>
                    </el-form-item>
                    <el-form-item prop="password" label="密码" label-width="70px">
                        <el-input size="medium" prefix-icon="el-icon-lock" show-password v-model="user.password"></el-input>
                    </el-form-item>
                    <!-- <el-form-item label="角色选择">
                        <el-radio-group v-model="user.role">
                            <el-radio :label="1">普通用户</el-radio>
                            <el-radio :label="2">医生</el-radio>
                        </el-radio-group>
                    </el-form-item> -->
                    <el-form-item prop="nickname" label="昵称" label-width="70px">
                        <el-input size="medium" prefix-icon="el-icon-user-solid" v-model="user.nickname"></el-input>
                    </el-form-item>
                    <el-form-item prop="phone" label="手机号" label-width="70px">
                        <el-input size="medium" prefix-icon="el-icon-phone" v-model="user.phone"></el-input>
                    </el-form-item>
                    <el-form-item prop="code" label-width="70px">
                        <el-input size="medium" style="width: 100px;" prefix-icon="el-icon-position"
                            v-model="user.code"></el-input>
                        <el-button type="" v-show="isShow" style="margin-left: 10px;" @click="sendMsg">发送验证码</el-button>
                        <el-button type="" v-show="!isShow" style="margin-left: 10px;" @click="sendMsg">{{
                            count }}秒后重新发送</el-button>
                    </el-form-item>
                    <el-form-item style="margin: 0px 0; text-align: right">
                        <el-button type="warning" size="small" autocomplete="off" @click="register">立即注册</el-button>
                        <el-button type="primary" size="small" autocomplete="off" @click="jumpLogin">返回登录</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
import { setRoutes } from "@/router";
export default {
    name: "",
    data() {
        return {
            user: { role: 1 },
            count: '',
            timer: null,
            isShow: true,
            rules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 3, max: 11, message: '长度在 3 到 11 个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
                ],
                nickname: [
                    { required: true, message: '请输入昵称', trigger: 'blur' },
                    { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
                ],
                phone: [
                    { required: true, message: '请输入手机号', trigger: 'blur' },
                    { min: 11, max: 11, message: '输入正确的手机号', trigger: 'blur' }
                ],
                code: [
                    { required: true, message: '请输入验证码', trigger: 'blur' },
                    { min: 4, max: 4, message: '长度在 4位', trigger: 'blur' }
                ],
            }
        }
    },
    methods: {
        created() {
        },
        jumpLogin() {
            this.$router.push("/login")
        },
        sendMsg() {
            this.request.post("/user/sendMsg", this.user).then(res => {
                if (res.code == 200) {
                    const num = 60;
                    if (!this.timer) {
                        this.count = num;
                        this.isShow = false;
                        this.timer = setInterval(() => {
                            if (this.count > 0 && this.count <= num) {
                                this.count--;
                            } else {
                                this.isShow = true;
                                clearInterval(this.timer);
                                this.timer = null;
                            }
                        }, 1000);
                    }
                }
            })
        },
        register() {
            this.$refs['userForm'].validate((valid) => {
                if (valid) {
                    this.request.post("/user/register", this.user).then(res => {
                        console.log("当前user===>", this.user)
                        if (res.code == 200) {
                            this.$message.success("注册成功")
                            this.$router.push("/login")
                        }
                        else {
                            console.log("res===>", res)
                            this.$message.error(res.msg)
                        }
                    })
                }
            })

        }
    }
}
</script>

<style>
.wrapper {
    height: 100vh;
    background-image: url(../assets/login.jpg);
    background-size: 100% 100%;
    background-attachment: fixed;
    overflow: hidden;
}
</style>